<template>
  <view id="msgDetail">
    <view v-if="msgStore.showType === 'list'" id="msg_list_box">
      <view class="title fs_18">
        <uni-icons class="back" @tap="goback" type="left" size="20px" />
        {{ msgStore.showType === "list" ? "预警" : "预警详情" }}
      </view>
      <scroll-view class="list_scroll" scroll-y>
        <view
          @tap.stop="haneleDetail(item)"
          @touchstart="touchStart($event, item)"
          @touchend="touchEnd($event, item)"
          class="msg_box"
          v-for="(item, index) in msgStore.messageBox.msgList"
          :key="item.MsgCode">
          <view class="msg_item" :class="{ delTime: item.delActive }">
            <view class="msg_title flex">
              <view class="tag flex">
                <view class="tag_icon">
                  <uni-icons type="sound-filled" color="#fff" size="22" />
                </view>
                <view class="fs_18">{{ item.Sender }}通知</view>
              </view>
              <view class="time fs_14">
                {{ item.SendTime }}
              </view>
            </view>
            <view class="msg_content fs_16 text_overflow">
              {{ item.MsgText }}
            </view>
          </view>
          <view
            @tap.stop="delMsgItem(item)"
            class="delete fs_16"
            :class="{ delactive: item.delActive }">
            删除
          </view>
        </view>
      </scroll-view>
      <view class="pagination">
        <zk-pagination
          :total="total"
          @event-change="handlePageChange"></zk-pagination>
      </view>
    </view>
    <view v-else id="msg_detail_box">
      <view class="title fs_18">
        <uni-icons class="back" @tap="goback" type="left" size="20px" />
        {{ msgStore.showType === "list" ? "预警" : "预警详情" }}
      </view>
      <view class="msg_item_detail flex">
        <view class="msg_title fs_20">
          {{ msgStore.showMsgItem.MsgTypeName }}
        </view>
        <view class="time_box flex fs_14">
          <view> 发布时间：{{ msgStore.showMsgItem.SendTime }} </view>
          <view> 处理时间：{{ msgStore.showMsgItem.ReceiveTime }} </view>
        </view>
        <view class="msg_content fs_18">
          {{ msgStore.showMsgItem.MsgText }}
        </view>
      </view>
    </view>
  </view>
  <zk-tips></zk-tips>
</template>

<script setup lang="ts">
import { useMessageStore } from "@/stores/modules/message"; //消息
import { onShow } from "@dcloudio/uni-app";
const total: any = ref(0);
const msgStore = useMessageStore();
const emit = defineEmits(["back"]);
onShow(async () => {
  const res: any = await msgStore.getMessageList({ MsgStatus: "" });
  total.value = res;
});
// 删除消息
const delMsgItem = async (item: any) => {
  await msgStore.delFindMsgList({ MsgCode: item.MsgCode });
};
// 点击消息详情
const haneleDetail = async (item: any) => {
  await msgStore.editFindMsgList({ MsgCode: item.MsgCode });
  msgStore.showType = "detail";
};
// 切换分页
const handlePageChange = async (e: any) => {
  const res: any = await msgStore.getMessageList({
    pageNum: e.pageNum,
    pageSize: e.pageSize,
    MsgStatus: "",
  });
  total.value = res;
};
let touchStartX: any = ref(0); // 触屏起始点x
let touchStartY: any = ref(0); //触屏起始点y
// 触摸开始
const touchStart = (e: any, item: any) => {
  touchStartX.value = e.touches[0].clientX;
  touchStartY.value = e.touches[0].clientY;
  console.log("触摸开始", item.MsgCode);
};
// 触摸结束
const touchEnd = (e: any, item: any) => {
  let deltaX = e.changedTouches[0].clientX - touchStartX.value;
  let deltaY = e.changedTouches[0].clientY - touchStartY.value;
  if (Math.abs(deltaX) > 50 && Math.abs(deltaX) > Math.abs(deltaY)) {
    if (deltaX >= 0) {
      console.log("左滑");
      item.delActive = false;
    } else {
      console.log("右滑");
      item.delActive = true;
      //   除了当前item 其余变为false
      msgStore.messageBox.msgList.forEach((el: any) => {
        if (el.MsgCode !== item.MsgCode) {
          el.delActive = false;
        }
      });
    }
  } else {
    item.delActive = false;
  }
};
// 点击返回按钮
const goback = async () => {
  if (msgStore.showType === "list") {
    emit("back");
  } else {
    msgStore.showType = "list";
    const res: any = await msgStore.getMessageList({ MsgStatus: "" });
    total.value = res;
  }
};
</script>

<style lang="scss" scoped>
#msgDetail {
  width: 100%;
  height: 100%;
  padding: 30px 20px 20px;
  #msg_list_box,
  #msg_detail_box {
    height: 100%;
    width: 100%;
    .title {
      position: relative;
      text-align: center;
      height: 24px;
      .back {
        position: absolute;
        top: 0;
        left: 0;
      }
    }
  }
  #msg_list_box {
    position: relative;
    .list_scroll {
      height: calc(100% - 64px);
      width: 100%;
      padding: 10px 0;
      .msg_item {
        width: 100%;
        display: flex;
        flex-direction: column;
        margin-bottom: 10px;
        &.delTime {
          transform: translateX(-80px);
          transition: 0.5s;
        }
        .msg_title {
          justify-content: space-between;
          margin: 10px 0;
          .tag {
            .tag_icon {
              display: flex;
              align-items: center;
              justify-content: space-around;
              width: 28px;
              height: 28px;
              background: rgb(246, 140, 100);
              border-radius: 50%;
              margin-right: 10px;
            }
          }
        }
        .msg_content {
          max-width: 100% !important;
          color: rgb(141, 143, 155);
          padding: 0 5px;
        }
      }
      .msg_box {
        display: flex;
        align-items: center;
        justify-content: space-between;
        height: 80px;
        width: 100%;
      }
      .delete {
        display: flex;
        width: 0px;
        height: 100%;
        align-items: center;
        justify-content: center;
        background: rgb(247, 59, 74);
        color: #ffffff00;
        transition: 0.5s;
        font-size: 0;
        &.delactive {
          width: 80px;
          color: #fff;
          font-size: 16px;
        }
      }
    }
    .pagination {
      width: 100%;
      margin: auto;
      position: absolute;
      bottom: 0;
    }
  }
  #msg_detail_box {
    .msg_item_detail {
      border-radius: 6px;
      margin-top: 20px;
      /* 描边/灰5 */
      background: rgb(244, 244, 244);
      padding: 24px;
      flex-direction: column;
      .time_box {
        color: rgb(173, 175, 184);
        margin: 20px 0;
      }
    }
  }
}
</style>
